#include "pollDownBtn.h"

pollDownBtn::pollDownBtn(const QString &text, const QString &hidePath, const QString &showPath, QWidget *parent) : QWidget(parent), hideIconPath(hidePath), showIconPath(showPath)
{

    textWidget = new QLabel();
    iconWidget = new QLabel();

    spaceWidget = new QWidget();
    HLayout = new QHBoxLayout();
    HLayout->setMargin(0);
    this->setLayout(HLayout);
    HLayout->addWidget(textWidget);
    HLayout->addWidget(spaceWidget);
    HLayout->addWidget(iconWidget);
    this->setFixedSize(pollDownSize);
    textWidget->setText(text);
    mousePressEvent(nullptr);
    iconWidget->setFixedSize(QSize(30, 30));
    //iconWidget->setStyleSheet("background-color:rgb(244, 255, 240);");

}

bool pollDownBtn::setBtnSize(const QSize &size)
{
    pollDownSize = size;
    //this->setBtnSize(pollDownSize);       // 这里应该调用错了，造成死循环
    return true;
}

bool pollDownBtn::setBtnIconSize(const QSize &size)
{
    pollDownIconSize = size;
    iconWidget->setFixedSize(pollDownIconSize);
    return true;
}

bool pollDownBtn::setBtnStylesheet(const QString &qss)
{
    if(qss.isEmpty())
    {
        DLOG("传入qss值为空");
        return false;
    }
    this->setStyleSheet(qss);
    return true;
}

bool pollDownBtn::setPaletteBF(const QPalette& palette)
{
    this->setPalette(palette);
    this->setAutoFillBackground(true);
    //遍历子组件然后更新他们的调色板
    foreach (QObject *child, this->children())
    {
        if(QWidget* childWidget = qobject_cast<QWidget*>(child))
        {
            childWidget->setPalette(palette);
            this->setAutoFillBackground(true);
        }
    }
    return true;
}

bool pollDownBtn::setLabelText(const QString& text)
{
    if(text.isEmpty())
    {
        DLOG("传入文本为空")
        return false;
    }
    //DLOG(tr("插入内容: %1").arg(text));
    textWidget->setText(text);
    return true;
}

void pollDownBtn::mousePressEvent(QMouseEvent *event)
{
    if(event != nullptr)
    QWidget::mousePressEvent(event);
    isVisible = !isVisible;
    if(isVisible)
    {
        QPixmap originalPixmap(showIconPath);
        iconWidget->setPixmap(originalPixmap.scaled(pollDownIconSize, Qt::KeepAspectRatio));
        //iconWidget->setStyleSheet(tr("border-image:url(%1);").arg(showIconPath));
    }else
    {
        QPixmap originalPixmap(hideIconPath);
        iconWidget->setPixmap(originalPixmap.scaled(pollDownIconSize, Qt::KeepAspectRatio));
    }
    emit middleWidgetIsVisible(isVisible);
}
